﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeachUser
{
    class Program
    {
        static void Main(string[] args)
        {
            UserInfo[] users = new UserInfo[]
            {
                new UserInfo ("Sergey", "Senjurv", 21, "Mogilev"),
                new UserInfo ("Dima", "Jilski", 23, "Mozir"),
                new UserInfo ("Nikolai", "Hohlov", 22, "Lida"),
                new UserInfo ("Sergey", "", 21, "Minsk")
            };

            HelperUserInfo helpUser = new HelperUserInfo(users.ToList<UserInfo>());

            foreach (var i in helpUser.SeachBySecondName("Sergey"))
            {
                Console.WriteLine(i.ToString());
            }

            foreach (var i in helpUser.SortByAgeOut())
            {
                Console.WriteLine(i.ToString());
            }

            Console.ReadLine();
        }
    }

    public class UserInfo
    {
        public String FirstName { get; set; }
        public String SecondName { get; set; }
        public Int32 Age { get; set; }
        public String Adress { get; set; }

        public UserInfo(String firstName, String secondName, Int32 age, String adress)
        {
            FirstName = firstName;
            SecondName = secondName;
            Age = age;
            Adress = adress;
        }

        public override string ToString()
        {
            return String.Format("пользователь {0} {1} возрастом {2} проживающий по адресу {3}", FirstName, SecondName, Age, Adress);
        }
    }

    public class HelperUserInfo
    {
        private List<UserInfo> spisok = new List<UserInfo>();

        public HelperUserInfo(List<UserInfo> users)
        {
            spisok = users;
        }

        public List<UserInfo> SeachByFirstName(String firstName)
        {
            var tempSpisok = from c in spisok where c.FirstName != "" select c; 
            var selectByName = from c in tempSpisok 
                               where c.FirstName == firstName 
                               select c;
            return selectByName.ToList<UserInfo>();
        }

        public List<UserInfo> SeachBySecondName(String secondName)
        {
            return spisok
                   .Where(i => i.SecondName != "")
                   .Select(i => i)
                   .Where(i => i.SecondName == secondName)
                   .Select(i => i)
                   .ToList<UserInfo>();
        }

        public List<UserInfo> SortByAge()
        {
            return (from c in spisok orderby c.Age select c).ToList<UserInfo>();
        }

        public List<UserInfo> SortByAgeOut()
        {
            return spisok.OrderBy(c => c.Age).ThenBy(c => c.FirstName).Select(c => c).ToList<UserInfo>();
        }
    }
}
